//
//  GradientButton.m
//  BingXinProduct
//
//  Created by skr on 2025/4/19.
//  Copyright © 2025 cat. All rights reserved.
//

#import "GradientButton.h"

@implementation GradientButton{
    CAGradientLayer *_gradientLayer;
}
- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        
        
        _gradientLayer = [CAGradientLayer layer];
//        _gradientLayer.frame = CGRectMake(0, 0, 20, 20);
        _gradientColors = @[[UIColor colorWithRed:197/255.0 green:121/255.0 blue:209/255.0 alpha:1], [UIColor colorWithRed:230/255.0 green:99/255.0 blue:162/255.0 alpha:1]];
        _startPoint = CGPointMake(0, 0.5);
        _endPoint = CGPointMake(1, 0.5);
        _gradientLayer.locations = @[@(0),@(1)];
        [self.layer insertSublayer:_gradientLayer atIndex:0];
//        [self.layer addSublayer:_gradientLayer];
//        [self setupButtonStyle];
    }
    return self;
}

- (void)setupButtonStyle {
//    self.layer.cornerRadius = 25;
//    self.layer.masksToBounds = YES;
//    [self setTitle:@"" forState:UIControlStateNormal];
//    self.titleLabel.font = [UIFont boldSystemFontOfSize:16];
}

- (void)layoutSubviews {
    [super layoutSubviews];
    _gradientLayer.frame = self.bounds;
    _gradientLayer.startPoint = _startPoint;
    _gradientLayer.endPoint = _endPoint;
    _gradientLayer.colors = [self gradientCGColors];

   
}

- (NSArray *)gradientCGColors {
    NSMutableArray *cgColors = [NSMutableArray array];
    
    for (UIColor *color in _gradientColors) {
        [cgColors addObject:(__bridge id)color.CGColor];
    }
  
    return cgColors;
}

// 添加点击效果
- (void)setHighlighted:(BOOL)highlighted {
    [super setHighlighted:highlighted];
    _gradientLayer.opacity = highlighted ? 0.7 : 1.0;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
